import { getCompoentPropsFn } from './form';

export const setTableColumnsFn = (
  columns: any = [],
  hasSelection = false,
  showAddAndDelBtn = true,
  routerType = 'add',
  sorter = false,
  width = 170
) => {
  const list: any = [];

  if (columns && columns.length) {
    if (hasSelection && showAddAndDelBtn && routerType !== 'check') {
      // 多选
      list.push({
        width: 60,
        type: 'selection',
        key: 'selection',
        fixed: 'left',
      });
    }

    for (const item of columns) {
      const {
        label = '',
        key = '',
        prop = '',
        itemData = null,
        checkProp = null,
        render = null,
        checkPropFn = null,
        required = false,
      } = item;

      if (label) {
        const newItemData = itemData ? { ...itemData } : null;

        const obj = {
          sorter,
          width,
          edit: !!itemData,
          ...item,
          key:
            routerType === 'check'
              ? checkProp || key || prop || label
              : key || prop || label || checkProp,
          required: routerType === 'check' ? false : required,
        };

        if (routerType !== 'check' || newItemData.component === 'UploadImage') {
          if (newItemData) {
            const { component = null } = newItemData;

            newItemData['component'] = component ? component : 'NInput';

            newItemData['componentProps'] = getCompoentPropsFn(newItemData);

            obj['itemData'] = newItemData;
          }
        } else {
          obj.edit = false;

          if (!render && checkPropFn) {
            obj['render'] = checkPropFn;
          }
        }

        // if (required) {
        //   obj.title = () => {
        //     return (
        //       h(
        //         'div',
        //         {
        //           display: 'flex',
        //           justifyContent: 'flex-start',
        //         },
        //         { default: () => 'Name' }
        //       ),
        //       [
        //         h('div', {}, [
        //           h('span', {
        //             class: 'tableTip',
        //             innerText: '*',
        //           }),
        //           h('span', {
        //             innerText: label,
        //           }),
        //         ]),
        //       ]
        //     );
        //   };
        // }
        list.push(obj);
      }
    }
  }

  return list;
};
